//
//  LoginCiewController.swift
//  hunan_ios_ldnsm
//
//  Created by I-Q-I on 2019/4/28.
//  Copyright © 2019 I-Q-I. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift
class RegisterViewController: UBaseViewController {
    
    var isEye = false
    var countdownTimer: Timer?
    var registerModel = RegisterModel()
    
    
    var registerView = RegisterView()
    var remainingSeconds: Int = 0 {
        willSet {
            registerView.sendButton.setTitle("\(newValue)秒", for: .normal)
            if newValue <= 0 {
                registerView.sendButton.setTitle("获取验证码", for: .normal)
                isCounting = false
            }
        }
    }
    var isCounting = false {
        willSet {
            if newValue {
                countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTime(_:)), userInfo: nil, repeats: true)
                remainingSeconds = 60
                registerView.sendButton.setTitleColor(UIColor.colOrange, for: .normal)
            } else {
                countdownTimer?.invalidate()
                countdownTimer = nil
            }
            registerView.sendButton.isEnabled = !newValue
        }
    }
    override func configNavigationBar() {
        super.configNavigationBar()
        navigationController?.navigationBar.isTranslucent = false
        navigationItem.title = "注册"
    }
    override func configUI() {
        
        registerView = RegisterView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        view.addSubview(registerView)
        registerView.sendButton.addTarget(self, action: #selector(sendButtonClick), for: .touchUpInside)
        registerView.backBtn.addTarget(self, action: #selector(pressBack), for: .touchUpInside)
        registerView.loginBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @objc func sendButtonClick(_ sender: UIButton) {
        IQKeyboardManager.shared.resignFirstResponder()
        let phone = registerView.phoneInput.text!.trim
        registerModel.sendCode(by: ["phone": phone,"type": 1], completion: {
            (result, message, data) in
            if result {
                 self.isCounting = true
                Toast.toastSuccess(message)
            } else {
                Toast.toastError(message)
            }
        })
    }
    @objc func updateTime(_ timer: Timer) {
        remainingSeconds -= 1
    }
    @objc func register(){
        IQKeyboardManager.shared.resignFirstResponder()
        let username = registerView.phoneInput.text!.trim
        let password = registerView.pwdInput.text!.trim
        let code = registerView.codeInput.text!.trim
        registerModel.register(by: ["username": username,"password":password, "code": code], completion: {
            (result, message, data) in
            if result {
                let user = data as! User
               UserDefaults.standard.setUser(userId: user.user_id, roleType: user.roleType, authStatus: user.authStatus, token: user.token)
            self.navigationController?.present(UNavigationController(rootViewController: LoginViewController()), animated: false, completion: nil)
            } else {
                Toast.toastError(message)
            }
        })
    }
}
